﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace SaturniLibrary.IO
{
    public class FileName
    {
        //非法字符列表
        private static readonly char[] InvalidFileNameChars = new[]
{
'"',
'<',
'>',
'|',
'\0',
'\u0001',
'\u0002',
'\u0003',
'\u0004',
'\u0005',
'\u0006',
'\a',
'\b',
'\t',
'\n',
'\v',
'\f',
'\r',
'\u000e',
'\u000f',
'\u0010',
'\u0011',
'\u0012',
'\u0013',
'\u0014',
'\u0015',
'\u0016',
'\u0017',
'\u0018',
'\u0019',
'\u001a',
'\u001b',
'\u001c',
'\u001d',
'\u001e',
'\u001f',
':',
'*',
'?',
'\\',
'/'
};

        //过滤方法
        public static string CleanInvalidFileName(string fileName)
        {
            fileName = fileName + "";
            fileName = InvalidFileNameChars.Aggregate(fileName, (current, c) => current.Replace(c + "", ""));
            if (fileName.Length > 1)
            {
                if (fileName[0] == '.')
                {
                    fileName = "dot" + fileName.TrimStart('.');
                }
            }
            return fileName;
        }

        /// <summary>
        /// 转换Url中的"/"为"\"
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string ConvertFromWebUrl(string url)
        {
            if (string.IsNullOrWhiteSpace(url))
            {
                return string.Empty;
            }

            return url.Replace('/', '\\');
        }

        public static string GenerateFileName(string folderPth, string fileName, string extension, int index = 0)
        {
            string fullName = Path.Combine(folderPth, fileName);
            if (index != 0)
            {
                fullName = fullName + index.ToString();
            }
            if (!string.IsNullOrEmpty(extension))
            {
                fullName = fullName + "." + extension;
            }

            if (File.Exists(fullName))
            {
                return GenerateFileName(folderPth, fileName, extension, ++index);
            }
            return fullName;
        }
    }
}
